package com.leidi.javacallspython.controller;

import com.leidi.javacallspython.enetity.vo.QstAnsVo;
import com.leidi.javacallspython.resp.Resps;
import com.leidi.javacallspython.service.CallPythonService;
import com.leidi.javacallspython.utils.AjaxResult;
import com.leidi.javacallspython.web.controller.BaseController;
import com.leidi.javacallspython.web.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigInteger;
import java.util.List;

@RestController
@RequestMapping("/robot")
@Api(value = "智能对话机器人", tags = "智能对话机器人")
public class CallPythonController extends BaseController {

  private final CallPythonService callPythonService;

  public CallPythonController(CallPythonService callPythonService) {
    this.callPythonService = callPythonService;
  }
  //
  //  /**
  //   * 储存数据
  //   *
  //   * @return AjaxResult
  //   */
  //  @PostMapping("/prepare/dataset/txt")
  //  @ApiOperation(value = "储存数据(txt)准备弃用")
  //  @ApiImplicitParams({
  //    @ApiImplicitParam(
  //        name = "questions",
  //        value = "问题（多个问题使用分割符'|'分割）",
  //        dataType = "string",
  //        paramType = "query"),
  //    @ApiImplicitParam(name = "answer", value = "问题答案", dataType = "string", paramType = "query")
  //  })
  //  public AjaxResult saveDatasetTxt(String questions, String answer) {
  //    return AjaxResult.success(callPythonService.saveDatasetTxt(questions, answer));
  //  }

  /**
   * 训练模型
   *
   * @return AjaxResult
   */
  @PostMapping("/training")
  @ApiOperation(value = "③.训练模型")
  public AjaxResult training() {
    return AjaxResult.success(callPythonService.training());
  }

  /**
   * 部署数据
   *
   * @return AjaxResult
   */
  @PostMapping("/prepare")
  @ApiOperation(value = "②.部署数据")
  public AjaxResult prepare() {
    return AjaxResult.success(callPythonService.prepare());
  }

  /**
   * 预测
   *
   * @return AjaxResult
   */
  @PostMapping(value = "/dialogue")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "query", value = "问题", dataType = "string", paramType = "query")
  })
  @ApiOperation(value = "④.预测")
  public AjaxResult predict(String query) {
    Resps resps = new Resps();
    resps.setFlag(0);
    String answer = callPythonService.autoQA(query);
    if (answer == null) {
      resps.setSay("对不起，我还没学会您说的");
      return AjaxResult.success(resps);
    }
    resps.setSay(answer);
    return AjaxResult.success(resps);
  }

  /**
   * 储存数据
   *
   * @return AjaxResult
   */
  @PostMapping("/save")
  @ApiOperation(value = "①.新增、更新数据")
  public AjaxResult saveDataset(@RequestBody QstAnsVo qstAnsVo) {
    return AjaxResult.success(callPythonService.saveDataset(qstAnsVo));
  }

  /**
   * 数据列表
   *
   * @return AjaxResult
   */
  @GetMapping("/list")
  @ApiOperation(value = "数据列表")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "question", value = "问题", dataType = "string", paramType = "query")
  })
  public TableDataInfo list(@RequestParam("question") String question) {
    startPage();
    List<QstAnsVo> qstAnsVos = callPythonService.selectQa(question);
    return getDataTable(qstAnsVos);
  }

  /**
   * 删除数据
   *
   * @return AjaxResult
   */
  @DeleteMapping("/{answerIds}")
  @ApiOperation(value = "①.删除数据")
  public AjaxResult remove(@PathVariable BigInteger[] answerIds) {
    return toAjax(callPythonService.deleteByIds(answerIds));
  }

  /**
   * 删除数据
   *
   * @return AjaxResult
   */
  @PostMapping("/save/document")
  @ApiOperation(value = "①.上传文档")
  public AjaxResult saveDocument(@RequestParam("files") MultipartFile files) {
    if (files.isEmpty()) {
      return AjaxResult.error("文件不能为空");
    }
    String result = callPythonService.parsingDocunment(files);
    return AjaxResult.success(result);
  }
}
